package com.esl.service.admin.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import com.esl.dao.admin.LoginLogDao;
import com.esl.dao.admin.UserDao;
import com.esl.dto.admin.PageInfo_Dto;
import com.esl.dto.admin.User_Dto;
import com.esl.model.admin.LoginLog;
import com.esl.model.admin.User;
import com.esl.service.admin.UserManager;

@Service("userManager")
public class UserManagerImpl implements UserManager {
	private UserDao userDao;
	private LoginLogDao loginLogDao;

	/*------------------------------------------*/
	@Override
	public void add(User user) {
		userDao.save(user);

	}

	@Override
	public User_Dto check(String studentNO, String password) {
		List<User> list = userDao.checkAndReturnUser(studentNO, password);
		if (!list.isEmpty())
			return list.get(0).toDto();

		else
			return new User_Dto(-1);
	}

	@Override
	public User getUserByID(int id) {
		return (User) userDao.findById(User.class, id);
	}

	@Override
	public PageInfo_Dto load(int pageNow, int pageSize, String hql,
			String orderBy) {
		return userDao.findByPageInfo(User.class, pageNow, pageSize, hql,
				orderBy);
	}

	@Override
	public Set<User> getCollectByString(String users) {
		Set<User> set = new HashSet<User>();
		for (String string : users.split(",")) {
			set.addAll(userDao.findByName(string));
		}
		return set;
	}

	@Override
	public void update(User user) {
		userDao.update(user);

	}

	@Override
	public void RecodeLogByLogin(String username, String loginIP) {
		loginLogDao.save(new LoginLog(username, loginIP));

	}

	@Override
	public boolean checkStudentNo(String studentNO) {
		List<User> list = userDao.checkAndReturnUser(studentNO);
		System.out.println("list:" + list.isEmpty());
		if (list.isEmpty())
			return true;
		else
			return false;
	}

	/*------------------------------------------*/
	@Resource
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	@Resource
	public void setLoginLogDao(LoginLogDao loginLogDao) {
		this.loginLogDao = loginLogDao;
	}

}
